<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/1/13 00:50
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */
use Wechat\Loader;
class Cash extends WX_Controller
{
    /**
     * 初始化
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 充值记录
     */
    public function cashInList()
    {
        if (!empty($this->openId)) {

            $this->load->model('common/Cash_Model', 'cash');
            $ret = $this->cash->getCashInRecord($this->userid);

            $result = [];
            if (!empty($ret)) foreach ($ret as $item) {
                $result[] = [
                    'id' => intval($item['id']),
                    'amount' => money_format('%i', $item['amount'] / 100),
                    'note' => value($item, 'note', ''),
                    'created' => value($item, 'created', '')
                ];
            }
            $this->output($result);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 充值
     */
    public function cashIn()
    {

        if (!empty($this->openId)) {
            $params = $this->input->post();
            if (empty($params['cashinType']) || empty($params['amount'])) {
                $this->outputError('参数错误');
            } else {

                if ($params['cashinType'] == 'system' && empty($params['mealId'])) {
                    $this->outputError('参数错误');
                }

                // 自定义
                if ($params['cashinType'] == 'custom') {
                    $this->load->model('common/CashMeal_Model', 'meal');
                    $mealid = $this->meal->createCustomMeal($this->userid, $params);
                    if (empty($mealid)) {
                        $this->outputError('自定义充值失败');
                    }
                    $params['mealId'] = $mealid;
                }

                // 系统套餐
                $this->load->model('common/Cash_Model', 'cash');
                $res = $this->cash->order($this->userid, $params, 'wx', 'wechat');
                if (!$res) {
                    $this->outputError($this->cash->getError());
                }

                Loader::config($this->options);
                $pay = Loader::get('pay');
                $notifyURL = APP_URL . '/wx/cashin';
                $prepayID = $pay->getPrepayId($this->openId, '丘丘谷', $res['sn'], $res['amount'], $notifyURL);
                if (!$prepayID) {
                    $this->outputError('下单失败请联系管理员');
                }
                $jsSDK = $pay->createMchPay($prepayID);
                $this->output([
                    'sdk' => $jsSDK,
                    'order' => $res
                ]);
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 获取套餐
     */
    public function getMeal()
    {
        if (!empty($this->openId)) {
            $this->load->model('common/CashMeal_Model', 'cash');
            $meals = $this->cash->getMeals();

            $data = [];
            if (!empty($meals)) foreach ($meals as $meal) {
                if ($meal['status'] != 6) {
                    continue;
                }

                $data[] = [
                    'id' => intval($meal['id']),
                    'name' => $meal['name'],
                    'amount' => intval($meal['amount']),
                    'amount_format' => money_format('%i', $meal['amount'] / 100)
                ];
            }

            $this->output([
                'data' => $data
            ]);

        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }


    /**
     * 申请提现
     */
    public function cashout()
    {
        if (!empty($this->openId)) {

            $amount = (int)$this->input->post('amount');
            if ($amount < 100) {
                $this->outputError('参数错误');
            }

            $this->load->model('wx/user/User_Model', 'user');
            $user = $this->user->getUserInfoById($this->userid);
            if (empty($user)) {
                $this->outputError('invalid request');
            }

            if ($user['amountUnLimit'] < $amount) {
                $this->outputError('可提现余额不足');
            }

            $this->load->model('common/Cash_Model', 'cash');
            if ($this->cash->applyCashout($user, $this->openId, $amount, 'wx')) {
                $this->output('申请成功,请等待审核');
            } else {
                $this->outputError('提现申请失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 充值
     */
    public function cashinDetail()
    {
        if (!empty($this->openId)) {
            $this->load->model('common/Cash_Model', 'cash');
            $res = $this->cash->getCashinDetail($this->userid);
            $this->output([
                'summary' => $this->cash->getSummary(),
                'lists' => $res
            ]);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 账单
     */
    public function cashDetail()
    {
        if (!empty($this->openId)) {
            $this->load->model('common/Cash_Model', 'cash');
            $audit = $this->cash->getCashoutDetail($this->userid);
            $this->output($audit);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 取现审核
     */
    public function cashoutApply()
    {
        if (!empty($this->openId)) {
            $this->load->model('common/Cash_Model', 'cash');
            $lists = $this->cash->getCashoutApplyList($this->userid, 0);
            $this->output($lists);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 提现记录
     */
    public function cashoutDetail()
    {
        if (!empty($this->openId)) {
            $this->load->model('common/Cash_Model', 'cash');
            $lists = $this->cash->getCashoutApplyList($this->userid, null);
            $this->output($lists);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

}
